package com.sxx.beautifulcity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.adapter.StoreDetailAdapter;
import com.sxx.beautifulcity.database.DetailStoreHelpter;
import com.sxx.beautifulcity.entity.greendao.Detail;
import com.sxx.beautifulcity.listenerInterface.OnDetailItemClickListener;

import java.util.List;

import io.github.codefalling.recyclerviewswipedismiss.SwipeDismissRecyclerViewTouchListener;

/**
 * Created by dllo on 15/11/9.
 */
public class StoreActivity extends AppCompatActivity implements OnDetailItemClickListener {
    private static final String TAG = StoreActivity.class.getSimpleName();
    //定义recyclerview
    private RecyclerView recyclerView;
    //定义收藏数据库帮助类
    private DetailStoreHelpter helpter;
    //定义适配器
    private StoreDetailAdapter adapter;
    private TextView titleTv, moreTv;
    private ImageView backIv;
    private List<Detail> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
        initView();
        //查询数据库  然后添加到适配器中
        list = helpter.queryDetailList();
        adapter.addData(list);
    }

    /**
     * 初始化
     */
    private void initView() {
        adapter = new StoreDetailAdapter(list);
        helpter = new DetailStoreHelpter();
        recyclerView = (RecyclerView) findViewById(R.id.store_recycler);
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        adapter.setOnDetailItemClickListener(this);

        titleTv = (TextView) findViewById(R.id.titleTv);
        titleTv.setText("我的收藏");
        moreTv = (TextView) findViewById(R.id.moreTv);
        moreTv.setText("");
        backIv = (ImageView) findViewById(R.id.backIV);
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StoreActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.left_out, R.anim.right_into);
            }
        });
/**
 * 滑动删除
 */
        SwipeDismissRecyclerViewTouchListener listener = new SwipeDismissRecyclerViewTouchListener.Builder(
                recyclerView,
                new SwipeDismissRecyclerViewTouchListener.DismissCallbacks() {
                    @Override
                    public boolean canDismiss(int position) {

                        return true;
                    }

                    @Override
                    public void onDismiss(View view) {
                        Log.d(TAG, "onDismiss() called with: " + "view = [" + view + "]");
                        // Do what you want when dismiss
                        int id = recyclerView.getChildPosition(view);
                        helpter.deleteById(list.get(id).getDetialId());
                        adapter.details.remove(id);
                        adapter.notifyDataSetChanged();
                    }
                })
                .setIsVertical(false)
                .setItemTouchCallback(
                        new SwipeDismissRecyclerViewTouchListener.OnItemTouchCallBack() {
                            @Override
                            public void onTouch(int index) {
                                Log.d(TAG, "onTouch() called with: " + "index = [" + index + "]");
                                // Do what you want when item be touched
                                Intent intent = new Intent();
                                intent.putExtra("url", list.get(index).getPath());
                                intent.setClass(StoreActivity.this, StoreDetailIntentActivity.class);
                                startActivity(intent);
                                finish();
                                overridePendingTransition(R.anim.left_out, R.anim.right_into);
                            }
                        })
                .create();
        recyclerView.setOnTouchListener(listener);
    }

    @Override
    public void setOnDetailItemClick(Detail data) {

    }
}
